//
//  WBImageView.m
//  WBImageView
//
//  Created by Tec_one on 15/12/16.
//  Copyright (c) 2015年 徐锐. All rights reserved.
//

#import "WBImageView.h"
#import "UIImageView+WebCache.h"
#import "WBImageController.h"
#import "UIView+ViewController.h"

#define kSpace 5
#define kWidth (self.bounds.size.width-20)/3

@implementation WBImageView

//有图片数据时创建图片
- (void)setImageUrls:(NSArray *)imageUrls{

    _imageUrls = imageUrls;
    
    if (_items == nil) {
        
        _items = [[NSMutableArray alloc]init];
    }
    
    [_items removeAllObjects];
    
    for (UIView *subView in self.subviews) {
        
        [subView removeFromSuperview];
    }
    
    /*_______________________________________________________________*/
    
    for (int i = 0; i<_imageUrls.count; i++) {
        
        WBImageViewItem *img = [[WBImageViewItem alloc]initWithFrame:[self frameFromIndex:i] withTarget:self action:@selector(itemTap:)];
        
        [img sd_setImageWithURL:[NSURL URLWithString:_imageUrls[i]]];
        
        img.index = i;
        
        //每创建一个item 就添加到数组中管理
        [_items addObject:img];
        
        [self addSubview:img];
        
    }
        
    self.height = ((_imageUrls.count-1)/3+1) * (kWidth + kSpace) + kSpace;


}

//通过下标获取图片的frame
- (CGRect)frameFromIndex:(int)index{

    CGFloat x = index%3 * kWidth + (index%3+1) * kSpace;
    CGFloat y = index/3 * kWidth + (index/3+1) * kSpace;

    return CGRectMake(x, y, kWidth, kWidth);
}

//图片的点击方法
- (void)itemTap:(WBTapGestureRecognizer *)tap{
    
    [self bringCellToFront];

    WBImageController *wbimgVC = [[WBImageController alloc]init];

    wbimgVC.dataList = _imageUrls;

    wbimgVC.index = tap.index;

    wbimgVC.currentItem = _items[tap.index];

    wbimgVC.wbImageView = self;

    [self.viewController presentViewController:wbimgVC animated:NO completion:^{

    }];

}


- (void)bringCellToFront{

    UIResponder *next = self.nextResponder;
    
    while (next) {
        
        if ([next isKindOfClass:[UITableViewCell class]]) {
            
            [[(UITableViewCell *)next superview]bringSubviewToFront:(UITableViewCell *)next];
            
            return;
        }
        
        next = next.nextResponder;
        
    }
    

}

@end

#pragma mark ----------------------------------------------------------------------------------------------


/**
 *  子类化的ImageView
 */
@interface WBImageViewItem ()
{
    WBTapGestureRecognizer *tap;
}

@end

@implementation WBImageViewItem

- (instancetype)initWithFrame:(CGRect)frame withTarget:(id)target action:(SEL)action{

    if (self = [super initWithFrame:frame]) {
        
//        self.contentMode = UIViewContentModeScaleAspectFit;
        
        self.userInteractionEnabled = YES;
        
        [self setContentMode:UIViewContentModeScaleAspectFill];

        self.clipsToBounds = YES;
        
        _originFrame = frame;
        
        tap = [[WBTapGestureRecognizer alloc]initWithTarget:target action:action];
        
        tap.numberOfTouchesRequired = 1;
        
        tap.numberOfTapsRequired = 1;
        
        tap.index = _index;
        
        [self addGestureRecognizer:tap];
        
    }
    return self;

}

- (void)setIndex:(int)index{

    _index = index;
    
    tap.index = _index;
}

@end


#pragma mark ----------------------------------------------------------------------------------------------

/**
 *  子类化的Tap手势
 */
@implementation WBTapGestureRecognizer

@end
